# SIS_VERSION=0.2.1
# SYMBIAN_UID=0xEDED0AF8

# This program is licensed under the GNU GPL v2 (see [COPYING]) and
# [http://www.python.org/download/releases/2.4.2/license/ Python Licenses]

# Copyright Thomas Johler (c) 2007

import sys
import os
import appuifw
import e32
import traceback

lck = e32.Ao_lock()

class MyStdout(object):
  def __init__(self):
    appuifw.app.body.color = 0
    
  def write(self, text):
    appuifw.app.body.add(unicode(text))

  def flush(self):
    pass

  def close(self):
    pass
    
class MyStderr(MyStdout):
  def write(self, text):
    c = appuifw.app.body.color
    appuifw.app.body.color = 0xf00000
    MyStdout.write(self, text)
    appuifw.app.body.color = c
  
def close():
  lck.signal()

appuifw.app.body = text = appuifw.Text()
appuifw.app.exit_key_handler = close

sys.stdout = MyStdout()
sys.stderr = MyStderr()

e32._stdo(u"C:\\pydict.err.txt")

try:
  try:
    import pydict
    pydict.DictionaryViewer().run(True)
    if text.get() == u'':
      close()
    else:
      appuifw.app.menu = [ (u'Exit', close) ]
  except:
    traceback.print_exc()
finally:
  appuifw.app.body = text;
  appuifw.app.exit_key_handler = close

lck.wait()
appuifw.app.exit_key_handler = None
appuifw.app.set_exit()
